/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CustomerOrderEntity;
import ejb.CustomerOrderEntityFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mohan
 */
@WebServlet(name = "ViewCustomerOrder", urlPatterns = {"/ViewCustomerOrder"})
public class ViewCustomerOrder extends HttpServlet {
    @EJB
    private CustomerOrderEntityFacade customerOrderEntityFacade;

    
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String cusOrderID = request.getParameter("cusOrderID");
        
        String orderNo = ""; 
        String due_date =  ""; 
        String comment =  ""; 
        String order_amount =  ""; 
        String customerID =  ""; 
        String customerName =  "";
        
        List customerOrder = customerOrderEntityFacade.findAll();
        
            for(Iterator it = customerOrder.iterator(); it.hasNext();)
            {
                //To get the order details of a specific customer order
                CustomerOrderEntity elem = (CustomerOrderEntity) it.next();
                if(elem.getOrderno().toString().equals(cusOrderID))
                {
                    orderNo = elem.getOrderno();
                    due_date = elem.getDueDate();
                    comment = elem.getComment();
                    order_amount = elem.getAmount()+"";
                    customerID = elem.getCustomerid().getCustomerid()+"";
                    customerName = elem.getCustomerid().getName();
                }
            }
        
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ViewCustomerOrder</title>");            
            out.println("</head>");
            out.println("<div align='center'>");
            out.println("<body bgcolor='#CCCCCC'>");
            out.println("<a href = 'Mainindex'>Main Window | </a>");
            out.println("<a href = 'AddCustomer'>Add new customer | </a>");
            out.println("<a href = 'ListCustomers'>View Customers | </a>");
            out.println("<a href = 'AddCustomerOrder'>Add new customer Order | </a>");
            out.println("<a href = 'ListCustomerOrders'>View Customers Orders</a>");
            out.println("<h1>Order Details of Order No : " +orderNo+"</h1>");
            
            out.println("<form>");
            out.println("<table width='600' height='300' border='1'>");
            out.println("<tr>");
            out.println("<td>Order No :</td><td><input type ='text' name ='orderNo' value='"+orderNo+"' readonly='readonly'></td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Customer Id :</td><td><input type ='text' name ='customerID' value='"+customerID+"' readonly='readonly'></td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Customer Name :</td><td><input type ='text' name ='customerName' value='"+customerName+"' readonly='readonly'></td>");            
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Amount : </td><td><input type ='text' name ='amount' value='"+order_amount+"' readonly='readonly'></td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Due Date : </td><td><input type ='text' name ='dueDate' value='"+due_date+"' readonly='readonly'></td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Comment : </td><td><textarea name = 'comment' readonly='readonly'>"+comment+"</textarea></td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("</form>");
            out.println("</body>");
            out.println("</div>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
